package merchantV1

import (
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"hc_server/pkg/app"
	"hc_server/routers/service/deviceService"
	"hc_server/runtime/e"
)

type DeviceBindParam struct {
	Type any `json:"type"` //操作类型：1-设备绑定；2-设备解绑
	Id   any `json:"id"`   //商户id
	Sn   any `json:"sn"`
}

// DeviceBind 设备绑定/解绑
func DeviceBind(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   DeviceBindParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	deviceService := deviceService.Device{
		MerchantId:    cast.ToInt(param.Id),
		BindingStatus: cast.ToString(param.Type),
		DeviceSn:      cast.ToString(param.Sn),
	}

	result, err := deviceService.MerchantDeviceBind()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", result)
}
